/*
 * MVCBasicListaIncremental.java
 *
 * Created on 13 de noviembre de 2008, 08:24 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */
package com.nextel.project.util;
 

import com.nextel.project.Configuracion;
import com.nextel.project.usuario.bean.GeneralBean;
import com.nextel.ui.OScrollList;
import com.nextel.ui.OSoftKey;
import com.nextel.ui.OUILook;
import pe.com.nextel.main.MVCAction;
import pe.com.nextel.util.ArrayList;
import pe.com.nextel.util.Iterator;
import pe.com.nextel.util.List;

/**
 *
 * @author eespinoza
 */
public abstract class MVCBasicListaIncremental extends MVCAction implements BuscarListener {

    private OSoftKey cmdSigue, cmdAtras;
    private List lstObject;
    private OScrollList lstMenu;
    //private Form form;
    private boolean flagForm;
    private boolean finalizo;
    private String tituloForm;
    private String backtitulo;
    private String siguetitulo;

    public MVCBasicListaIncremental(String titulo, String nameSigue, String nameback) {

        finalizo = false;

        this.tituloForm = titulo;
        this.backtitulo = nameback;
        this.siguetitulo = nameSigue;
 
    }

    protected void createView()  {

        cmdSigue = new OSoftKey(siguetitulo);
        cmdAtras = new OSoftKey(backtitulo);


     //   form = new Form(tituloForm);
        formulario.setTitle(tituloForm, OUILook.PLAIN_SMALL_TITLE);

        formulario.addSoftKey(cmdSigue);
        formulario.addSoftKey(cmdAtras);

    }

//    protected Displayable getScreen() {
//        return form;
//    }

    protected void destroy() {
        super.destroy();
        if (this.lstObject != null) {
            this.lstObject.clear();
            this.lstObject = null;
        }

//        if (lstMenu != null) {
//            lstMenu.destroy();
//            lstMenu = null;
//        }
//            form.deleteAll();
       
    }

    protected void createItems() {
 
        finalizo = false;

        if (lstObject == null)lstObject = new ArrayList();
        
        int separador = Configuracion.ALTOITEM - 7;

        int rrows = formulario.getHeight() / separador;

//      lstMenu = new ListItem(new ArrayList(), null, rrows, 0, separador);
//      lstMenu.setPreferredSize(form.getWidth(), form.getHeight());

        lstMenu = new OScrollList(formulario.getWidth(),formulario.getHeight(),
                    OUILook.PLAIN_SMALL_SOFTKEY, OUILook.PLAIN_SMALL_TITLE );

        lstMenu.isWithBorder = false;
        formulario.add(lstMenu);

        formulario.setTitle(tituloForm);
        
        flagForm = true;
    }

    public void performAction(OSoftKey c) {
//        System.out.println("MVCBasicListaIncremental.commandAction.flagForm" + flagForm);
        if (flagForm) {

            if (c == cmdSigue) {

                if (lstObject.size() > 0) {

                    flagForm = false;
                    seguir(getSeleccionado());
                    finalizo = true;
                    destroy();

                }

            } else if (c == cmdAtras) {
                flagForm = false;
                atras();
                finalizo = true;
                destroy();

            }
        }
    }

    protected GeneralBean getSeleccionado() {
        //return (GeneralBean) lstObject.get(lstMenu.getIndxLista());
        return (GeneralBean) lstObject.get(lstMenu.getSelectedIndex());
    }

    public void agregarElementosVectorDatos(List lista) {
//         System.out.println("agregarElementosVectorDatos. lista.size=="+ lista.size());

        if (!finalizo) { // si no finaliza la busqueda o se interrumpe

            //List listaArrayList = new ArrayList();
            String[] datos = new String[lista.size()];
            
            GeneralBean bean;

            //for (Iterator it = lista.iterator(); it.hasNext();) {
            for(int i=0; i < lista.size();i++){

                //bean = (GeneralBean) it.next();
                bean = (GeneralBean) lista.get(i);
                datos[i] =  bean.getDescripcion();

              // listaArrayList.add(bean.getDescripcion());

                lstObject.add(bean.getDescripcion());

            }

           // lstMenu.addList(listaArrayList);
            lstMenu.populate(datos);
            lista.clear();
            lista = null;
            bean = null;
            formulario.repaint();

//            listaArrayList.clear();
//            listaArrayList = null;
//
//            bean = null;

        }

    }

    public void finalizo() {
//        System.out.println("finalizo");
        finalizo = false;

        if (formulario != null && lstObject != null) {
            formulario.setTitle(lstObject.size() + tituloForm);
        }

    }

    public boolean esFinalizadaBusqueda() {
        return finalizo;
    }

    public abstract void seguir(GeneralBean general);

    public abstract void atras();
}
